<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\CompanyWt;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class CompanyController extends BaseController
{
    public function actionCompanyWtList(){
        $page = Yii::$app->request->post('page', 0);
        $name = Yii::$app->request->post('name', '');
        $company_query = CompanyWt::find()->where(['status' => [0,1]]);
        if ($name) {
            $company_query->andWhere(['like', 'name', $name]);
        }
        $company_lisy = $company_query->orderBy("created_at desc")->limit(CompanyWt::PAGE_SIZE)->offset(CompanyWt::PAGE_SIZE*$page)->all();
        $company_data = [];
        if ($company_lisy) {
            foreach ($company_lisy as $v) {
                $company_data[] = [
                    'id' => $v->id,
                    'name' => $v->name,
                    'status' => $v->status,
                    'created_time' => date("Y-m-d", $v->created_at),
                ];
            }
        }
        return Re::data([
            'data' => $company_data,
            'totalPage' => $company_query->count(),
            'currentPage' => $page,
            'perPage' => CompanyWt::PAGE_SIZE
        ]);
    }

    public function actionEditCompanyWt(){
        $id = Yii::$app->request->post('id', 0);
        $post = Yii::$app->request->post();
        return $this->edit(CompanyWt::class, $id, $post);
    }

    public function actionChangeCompanyWtStatus(){
        $id = Yii::$app->request->post('id', 0);
        $status = Yii::$app->request->post('status', 0);
        $companyInfo = CompanyWt::findOne($id);
        if (empty($companyInfo)) {
            return Re::data(null, '委托单位息读取失败', Re::ERROR);
        }
        $companyInfo->status = $status;
        if (!$companyInfo->save()) {
            return Re::data(null, '操作失败', Re::ERROR);
        }
        return Re::data();
    }
}
